package com.cskaoyan.config;

import com.cskaoyan.shiro.AdminRealm;
import com.cskaoyan.shiro.CustomWebSessionManager;
import com.cskaoyan.shiro.WxRealm;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.LinkedHashMap;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(securityManager);
        //对请求过滤 filter
        //key 请求url value 对应的过滤器
        //执行链有顺序，所以使用LinkedHashMap
        LinkedHashMap<String, String> filterMap = new LinkedHashMap<>();
        filterMap.put("/login.jsp", "anon");
        filterMap.put("/admin/auth/login", "anon"); //匿名
        //filterMap.put("/admin/auth/info", "anon"); //匿名
        filterMap.put("/wx/auth/login", "anon"); //匿名
        filterMap.put("/wx/auth/*", "anon"); //匿名
        filterMap.put("/wx/home/*", "anon"); //首页
        filterMap.put("/wx/goods/*", "anon"); //商品
        filterMap.put("/wx/brand/*", "anon"); //品牌
        filterMap.put("/wx/catalog/*", "anon"); //类目
        filterMap.put("/wx/search/*", "anon"); //搜索
        filterMap.put("/wx/comment/*", "anon");
        filterMap.put("/wx/storage/*", "anon");
        filterMap.put("/wx/topic/*", "anon"); //专题
        filterMap.put("/wx/cart/*", "anon"); //专题
//        filterMap.put("/wx/coupon/selectlist", "anon");
        //filterMap.put("/index","anon");
        filterMap.put("/**", "authc"); //认证

        shiroFilterFactoryBean.setFilterChainDefinitionMap(filterMap);

        return shiroFilterFactoryBean;
    }

    //shiro核心组件 securityManager
    @Bean
    public DefaultWebSecurityManager securityManager(AdminRealm adminRealm, DefaultWebSessionManager sessionManager, CustomAuthenticator authenticator) {
//    public DefaultWebSecurityManager securityManager(AdminRealm adminRealm, DefaultWebSessionManager sessionManager) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(adminRealm);
        defaultWebSecurityManager.setSessionManager(sessionManager);
        defaultWebSecurityManager.setAuthenticator(authenticator);
        return defaultWebSecurityManager;

    }

    @Bean
    public DefaultWebSessionManager sessionManager() {
        DefaultWebSessionManager defaultWebSessionManager = new CustomWebSessionManager();
        defaultWebSessionManager.setGlobalSessionTimeout(1000 * 60 * 60 * 6);
        defaultWebSessionManager.getSessionIdCookie().setMaxAge(1000 * 60 * 60 * 6);
        return defaultWebSessionManager;
    }

    /**
     * 声明式鉴权  注解需要的组件
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(DefaultWebSecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }


    @Bean
    public CustomAuthenticator authenticator(AdminRealm adminRealm, WxRealm wxRealm) {
        CustomAuthenticator customAuthenticator = new CustomAuthenticator();
        ArrayList<Realm> realms = new ArrayList<>();
        realms.add(adminRealm);
        realms.add(wxRealm);
        customAuthenticator.setRealms(realms);
        return customAuthenticator;
    }
}
